package sys

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server/app/api/util"
	"sieve_admin_server/app/contextx"
	"sieve_admin_server/app/schema"
	"sieve_admin_server/app/server"
	serviceSys "sieve_admin_server/app/service/sys"
)

var CountrySet = wire.NewSet(wire.Struct(new(CountryAPI), "*"))

type CountryAPI struct {
	util.EmptyAPI
	CountrySrv *serviceSys.CountrySrv
}

func (a *CountryAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "country",
			Title: "国家管理", Perms: []*server.Perm{
				{Name: "add", Title: "新增"},
				{Name: "query", Title: "查询"},
				{Name: "queryCountries", Title: "查询国家列表"},
				{Name: "edit", Title: "修改"},
				{Name: "enable", Title: "启用"},
				{Name: "disable", Title: "禁用"},
				{Name: "del", Title: "删除"},
			},
			Order: 9,
		},
	}
}

func (a *CountryAPI) API_add_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("full_name"),
				contextx.NewVar("short_name"),
				contextx.NewVar("area_code"),
				contextx.NewVar("number_bit_count", true),
			},
		)
	)

	return a.CountrySrv.Create(context.Background(), reqArgs)
}

func (a *CountryAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	return a.CountrySrv.Query(context.Background(), reqArgs, p)
}

func (a *CountryAPI) API_queryCountries_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{},
		)
		repKeys = []string{"id", "full_name", "short_name", "area_code", "number_bit_count"}
	)

	return a.CountrySrv.QueryCountryList(context.Background(), reqArgs, repKeys)
}

func (a *CountryAPI) API_enable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.CountrySrv.Enable(context.Background(), reqArgs)
}

func (a *CountryAPI) API_disable_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.CountrySrv.Disable(context.Background(), reqArgs)
}

func (a *CountryAPI) API_edit_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("full_name"),
				contextx.NewVar("short_name"),
				contextx.NewVar("area_code"),
				contextx.NewVar("number_bit_count", true),
				contextx.NewVar("disable_sieve_list"),
			},
		)
	)

	return a.CountrySrv.Update(context.Background(), reqArgs)
}

func (a *CountryAPI) API_del_item(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
			},
		)
	)

	return a.CountrySrv.Delete(context.Background(), reqArgs)
}
